package cn.imu.appdevexperiment2020._3activity.chat;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020._3activity.chat.bean.Msg;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-11-13 19:09
 */
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
   private List<Msg> mMsgList;

   static class ViewHolder extends RecyclerView.ViewHolder {
      LinearLayout leftLayout;
      LinearLayout rightLayout;
      TextView leftMsg;
      TextView rightMsg;

      public ViewHolder(View view) {
         super(view);
         leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
         rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
         leftMsg = (TextView) view.findViewById(R.id.left_msg);
         rightMsg = (TextView) view.findViewById(R.id.right_msg);
      }
   }

   public MsgAdapter(List<Msg> msgList) {
      mMsgList = msgList;
   }

   @Override
   public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false);
      return new ViewHolder(view);
   }

   @Override
   public void onBindViewHolder(ViewHolder holder, int position) {
      Msg msg = mMsgList.get(position);
      if (msg.getType() == Msg.TYPE_RECEIVED) {
         holder.leftLayout.setVisibility(View.VISIBLE);
         holder.rightLayout.setVisibility(View.GONE);
         holder.leftMsg.setText(msg.getContent());
      } else if (msg.getType() == Msg.TYPE_SENT) {
         holder.rightLayout.setVisibility(View.VISIBLE);
         holder.leftLayout.setVisibility(View.GONE);
         holder.rightMsg.setText(msg.getContent());
      }
   }

   @Override
   public int getItemCount() {
      return mMsgList.size();
   }
}
